//
//  SDDatePickerView.m
//  PK10
//  Created by Mac01 on 16/9/27.
//  readMe:
//  使用方法－
// - (void)selectDate
// {
//    /*************** 时间选择器 ***************/
//    _datePickerView = [[SDDatePickerView alloc]init];
//    _datePickerView.delegate = self;
// }
//
// #pragma mark - pickerViewDelegate  传出date值
// - (void)sureBtnWithDate:(NSString *)dateStr
// {
//    NSLog(@"%@",dateStr);
// }

#import "SDDatePickerView.h"
//#import "UIView+UUFrame.h"
#import "UIView+Frame.h"

@interface SDDatePickerView()

@property (nonatomic, strong)UIDatePicker *datePicker;
@property (nonatomic, strong) UIView *coverView;


@property (nonatomic, strong)NSString *dateStr;

@end

@implementation SDDatePickerView

- (instancetype)init
{
    if ([super init])
    {
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
//        [self createUIWith:frame];
    }
    return self;
}

- (void)createUI
{
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 238.5);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    /*************** 移动到指定位置 ***************/
    [UIView animateWithDuration:0.4 animations:^{
        self.frame = CGRectMake(0, SCREEN_HEIGHT - 238.5, SCREEN_WIDTH, 238.5);
    }];
    /*************** 蒙板 ***************/
    _coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _coverView.alpha = 0.3;
    _coverView.backgroundColor = [UIColor blackColor];
    [window addSubview:_coverView];
    [window addSubview:self];
    [window bringSubviewToFront:self];
    
    
    /*添加手势事件,移除View*/
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldResign)];
    [_coverView addGestureRecognizer:tapGesture];
    
    
    UIView *bgVeiw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    bgVeiw.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:bgVeiw];

    /*************** 按钮 ***************/
    UIButton *cancelBtn = [self createButtonWithFrame:CGRectMake(14, 9, 48, 27) borderColor:RGB(197, 197, 197) superView:bgVeiw title:@"取消"];
    [cancelBtn setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
     [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sureBtn = [self createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 14 - 48, 9, 48, 27) borderColor:RGB(197, 197, 197) superView:bgVeiw title:@"确定"];
    [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.backgroundColor = RGB(206, 24, 36);
    
    /*************** datePicker ***************/
    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, bgVeiw.bottom, SCREEN_WIDTH, 389/2)];
    _datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_datePicker addTarget:self action:@selector(dataChanged:) forControlEvents:UIControlEventValueChanged];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    [self addSubview:_datePicker];
    //区域 时间
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    _datePicker.locale = locale;
//    //利用格里高利日历方法计算偏差后的日期时间
//    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    //设置最小时间
    NSString* minimumStr = @"20090101000000";
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate* minimumDate = [inputFormatter dateFromString:minimumStr];
    self.datePicker.minimumDate = minimumDate;
    //目前时间
    NSDate *localDate = [NSDate date];
    //超过最大时间会置灰
    self.datePicker.maximumDate = localDate;
    
   
}



/**
 *  滚动时触发的事件
 *
 *  @param sender
 */
- (void)dataChanged:(id)sender
{
    UIDatePicker *control = (UIDatePicker *)sender;
    NSDate *date = control.date;
    NSLog(@"dateChanged响应事件%@",date);
    
    //获取用户设置的时间
    NSDate *pickerDate = [self.datePicker date];
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc]init];
    [pickerFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    _dateStr = dateString;
    
    //打印显示日期时间
    NSLog(@"格式化显示时间：%@",_dateStr);
}


- (void)cancelAction
{
    [self.delegate cancelBtnClick];
    NSLog(@"cancelAction");
    [self dismissContactView];
}

- (void)sureAction
{
    NSLog(@"sureAction");
    
    if ([self.delegate respondsToSelector:@selector(sureBtnWithDate:)])
    {
        if ([_dateStr isEqualToString:@""]||_dateStr == nil)
        {
            NSDate *todayDate = [NSDate date];
            NSDateFormatter *todayFormatter = [[NSDateFormatter alloc]init];
            [todayFormatter setDateFormat:@"yyyy-MM-dd"];
            NSString *todayStr = [todayFormatter stringFromDate:todayDate];
             [self.delegate sureBtnWithDate:todayStr];
        }
        else
        {
            [self.delegate sureBtnWithDate:_dateStr];
        }
    }
    [self dismissContactView];
}
-(void)textFieldResign{
    [self.delegate cancelBtnClick];
    [self dismissContactView];
}

#pragma mark - 手势点击事件,移除View
- (void)dismissContactView
{
    [UIView animateWithDuration:0.2 animations:^{
        _coverView.alpha = 0;
    } completion:^(BOOL finished) {
        [_coverView removeFromSuperview];
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 477/2);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/*************** 创建UIButton ***************/
- (UIButton *)createButtonWithFrame:(CGRect)frame borderColor:(UIColor *)borderColor superView:(UIView *)superView title:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    btn.layer.borderColor = borderColor.CGColor;
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 6;
    btn.layer.masksToBounds = YES;
    [btn setTitle:title forState:UIControlStateNormal];
    [superView addSubview:btn];
    return btn;
}

@end




